﻿using System;

// 泛型节点类
public class Node<T>
{
    public T Data { get; set; }
    public Node<T> Next { get; set; }

    public Node(T data)
    {
        Data = data;
        Next = null;
    }
}

// 泛型链表类
public class LinkedList<T>
{
    private Node<T> _head;

    // 添加元素方法
    public void Add(T data)
    {
        Node<T> newNode = new Node<T>(data);
        if (_head == null)
        {
            _head = newNode;
        }
        else
        {
            Node<T> current = _head;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }

    // ForEach 方法定义在此处
    public void ForEach(Action<T> action)
    {
        Node<T> current = _head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main()
    {
        LinkedList<int> list = new LinkedList<int>();
        list.Add(10);
        list.Add(20);
        list.Add(30);

        // 使用 ForEach 打印元素
        list.ForEach(x => Console.Write(x + " ")); // 输出: 10 20 30
    }
}